# ggplot2 - scale
library(dplyr)
library(ggplot2)

# 1. Basic Scale ----

## 1.1 Axis ticks
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = class))

# Same as above
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = class)) +
  scale_x_continuous() +
  scale_y_continuous() +
  scale_color_discrete()

# Set the scale of Y-axis
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_y_continuous(breaks = seq(15, 40, by = 5))

# No ticks for X and Y axis
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_x_continuous(labels = NULL) +
  scale_y_continuous(labels = NULL)

pp <- presidential %>% 
  mutate(id = 33 + row_number())
ggplot(pp, aes(start, id)) +
  geom_point() +
  geom_segment(aes(xend = end, yend = id)) +
  scale_x_date(NULL, breaks = presidential$start, date_labels = "'%y")


## 1.2 Legend layout
base <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = class))

# Legend position
base + theme(legend.position = "left")
base + theme(legend.position = "right")
base + theme(legend.position = "top")
base + theme(legend.position = "bottom")
base + theme(legend.position = "none")

# Individual legend
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = class)) +
  geom_smooth(se = FALSE) +
  theme(legend.position = "bottom") +
  guides(color = guide_legend(nrow = 1, 
                              override.aes = list(size = 4)))

## 1.3 Replacing scale
ggplot(diamonds, aes(carat, price)) +
  geom_bin2d()

ggplot(diamonds, aes(log10(carat), log10(price))) +
  geom_bin2d()

ggplot(diamonds, aes(carat, price)) +
  geom_bin2d() +
  scale_x_log10() + scale_y_log10()

# color
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = drv))

# ColorBrewer
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = drv)) +
  scale_color_brewer(palette = "BrBG")

ggplot(pp, aes(start, id, color = party)) +
  geom_point() +
  geom_segment(aes(xend = end, yend = id)) +
  scale_color_manual(
    values = c(Republican = "red", Democratic = "blue")
  )

## 1.4 Zooming
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = class)) +
  geom_smooth() +
  coord_cartesian(xlim = c(5, 7), ylim = c(10, 30))

a <- ggplot(mpg, aes(displ, hwy)) +
    geom_point(color = "red", size = 3, aes(alpha = factor(cyl)))
a

b <- ggplot(mpg, aes(displ, hwy)) +
    geom_point(color = "red", size = 3, aes(alpha = factor(cyl))) +
    scale_alpha_discrete(range = c(0.1, 0.2))
    # Set alpha transfer (the range of alpha)
b

# scale_colour_manual(values = c("type1" = "color1", ...))
p <- ggplot(mtcars, aes(mpg, wt)) +
    geom_point(size = 4, aes(color = factor(cyl)))
p + scale_color_manual(values = c("8" = "red", "4" = "blue", "6" = "green"))

# Gray scale
p <- ggplot(mtcars, aes(mpg, wt)) +
    geom_point(size = 4, aes(colour = factor(cyl)))
p + scale_color_grey(start = 0.5, end = 1)
p + scale_color_grey(start = 0.5, end = 0.7)


p <- ggplot(mtcars, aes(mpg, wt)) +
    geom_point(size = 4, aes(colour = drat))
p + scale_color_gradient(low = "#FFFF00", high = "#FF0000")

p + scale_color_gradient2(
    low = "yellow",
    mid = "white",
    high = "red",
    midpoint = 4
)


#::::::::::::::::::::::::::::::::::

p <- qplot(cty, hwy, data = mpg)
p + aes(x = drv)
p + aes(x = drv) + scale_x_discrete()

p <- qplot(sleep_total, sleep_cycle, data = msleep, colour = vore)
p
p + scale_colour_hue("What does\nit eat?",
                     breaks = c("herbi", "carni", "omni", NA),
                     labels = c("platns", "meat", "both", "don't know"))

p + scale_colour_brewer(palette = "Set1")

# Common parameters for scale
p <- qplot(cty, hwy, data = mpg, colour = displ)
p
p + scale_x_continuous("City mpg")  # Update x label
p + xlab("City mpg")
p + ylab("Highway mpg")
p + labs(x = "City mpg", y = "Highway mpg", colour = "Displacement")
p + xlab(expression(frac(miles, gallon)))

p <- qplot(cyl, wt, data = mtcars)
p
p + scale_x_continuous(breaks = c(5.5, 6.5))
p + scale_x_continuous(limits = c(5.5, 6.5))
p
p + scale_colour_gradient(breaks = c(5.5, 6.5))
p + scale_colour_gradient(limits = c(5.5, 6.5))

qplot(log10(carat), log10(price), data = diamonds)
qplot(carat, price, data = diamonds) +
  scale_x_log10() + scale_y_log10()


#::::::::::::::::::::::::::::::::::::::::::

p + scale_color_gradientn(colors = terrain.colors(10))
p + scale_color_gradientn(colors = gray.colors(10))
p + scale_color_gradientn(colors = heat.colors(10, alpha = I(0.1)))
p + scale_color_gradientn(colors = topo.colors(7))
p + scale_color_gradientn(colors = rainbow(6))
p + scale_color_gradientn(colors = cm.colors(7))

# Size scale
ggplot(data = mpg, aes(x = displ, y = hwy)) +
    geom_point(color = "darkblue", aes(size = cyl)) +
    scale_size(range = c(1, 9))

ggplot(data = mpg, aes(x = displ, y = hwy)) +
    geom_line(size = 1.5, aes(group = fl, linetype = fl, color = fl)) +
    scale_linetype_manual(values = 1:5)

ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point(size = 5) +
    scale_x_continuous(trans = "log")
ggplot(data = mpg, aes(x = log(displ), y = hwy)) + geom_point(size = 5)

ggplot(data = mpg, aes(x = factor(cyl), y = hwy)) +
    geom_point(size = 5, aes(color = factor(cyl))) +
    scale_x_discrete(name = 'Cylinders',
        labels = c("A", "B", "C", "D", "E"), expand = c(0, 3))


last_month <- Sys.Date() - 0:29
df <- data.frame(
    date = last_month,
    price = runif(30))
base <- ggplot(df, aes(date, price)) +
    geom_line()

base + scale_x_date(date_labels = "%b%d")
base + scale_x_date(date_breaks = "1 week", date_labels = "%W")
base + scale_x_date(date_minor_breaks = "1 day")

g <- ggplot(data = mpg, aes(x = displ, y = hwy)) +
    geom_point(size = 4, aes(color = factor(cyl)))
g
g + scale_color_hue(name = "Number of cylinders", limits = c(5, 6, 8))
g + scale_color_hue(name = "Number of cylinders", limits = c(5, 6, 8),
                    breaks = c(6, 8))
g + scale_color_hue(name = "Number of cylinders", limits = c(5, 6, 8),
    breaks = c(6, 8), labels = c("Cylinders=6", "Cylinders=8"))
g + scale_color_hue(guide = FALSE)
g + scale_color_hue(name = "Number of cylinders", limits = c(5, 6, 8),
                    na.value = "white")
